package com.gitee.feizns.quickstart.mybatis.plus.utils;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gitee.feizns.dynamic.Strings;
import com.gitee.feizns.dynamic.bean.Props;
import com.gitee.feizns.quickstart.spring.AppUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author feizns
 * @since 2025/7/19
 */
public class Mappers {

    /**
     * 映射器
     * @param type 类型
     * @return {@link BaseMapper }<{@link T }>
     */
    @SuppressWarnings("unchecked")
    public static <T> BaseMapper<T> of(Class<T> type) {
        return (BaseMapper<T>) AppUtils.getBean(Strings.firstLower(type.getSimpleName()) + "Mapper", BaseMapper.class);
    }

    /**
     * 组合两个类中都重合的属性并返回属性名列表
     * @param sourceType 源类型
     * @param voType 响应对象类型
     * @return {@link List }<{@link String }>
     */
    public static List<String> intersectionPropNames(Class<?> sourceType, Class<?> voType) {
        Set<String> sourcePropNames = Props.names(sourceType).collect(Collectors.toSet());
        return Props.names(voType).filter(sourcePropNames::contains).collect(Collectors.toList());
    }

}
